package com.qidian.mes.produce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.mes.pojo.Composition;
import com.qidian.mes.pojo.Operate;
import com.qidian.mes.produce.mapper.CompositionMapper;
import com.qidian.mes.produce.service.CompositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 陈俊成
 * @Company ccc
 * @Description
 * @Time 2024/10/21 11:16
 */
@Service
public class CompositionServiceImpl implements CompositionService {
    @Autowired
    CompositionMapper compositionMapper;

    @Override
    public MESResult queryAllByRelId(int relId, int page, int limit) {
        PageHelper.startPage(page,limit);
        List<Composition> compositions = compositionMapper.getList(relId);

        PageInfo<Composition> compositionPageInfo = new PageInfo<>(compositions);
        return MESResult.success(compositionPageInfo);
    }

    @Override
    public MESResult addComposition(Composition composition) {
        compositionMapper.addComposition(composition);
        List<Composition> compositions  = compositionMapper.getList(composition.getCompositionCraftId());
        for (int i=0;i<compositions.size()-1;i++){
            if (compositions.get(i+1).getCompositionOrder()==composition.getCompositionOrder()){
                System.out.println("+++13++++++");
                compositionMapper.updateNext(composition.getCompositionName(),compositions.get(i).getCompositionRelId());
                break;
            }
        }
        return MESResult.success();
    }

    @Override
    public MESResult primaryComposition(Composition composition) {
        if (compositionMapper.update(composition)==1){
            return MESResult.success();
        }
        return MESResult.error();
    }

    @Override
    public MESResult getPrimary(int compositionRelId) {
        return MESResult.success(compositionMapper.getUpdate(compositionRelId));
    }
}
